% clear workspace
clear;
clc;

% get file abs path
[filename, filepath] = uigetfile('.jpg', '请选择一张图片');
abs_path = strcat(filepath, filename);

% load image data
try
  img_data = imread(abs_path);
catch err
  
  % exit this process
  fprintf(strcat(err.message, '\r\n'));
  return;
end

% height, witdh, color channal
[img_h img_w img_c] = size(img_data);

figure('name', '原图');
imshow(img_data);
title('原图');

% initialize the picture channals
img_kinds.gray = {'GREY'};
img_kinds.colors = {'RED', 'GREEN', 'BLUE'};
img_kinds.opacity = {'RED', 'GREEN', 'BLUE', 'ALPHA'};

switch img_c
  case 1
    img_kind = img_kinds.gray;
    img_title = '灰度图单通道直方图';
  
  case 3
    img_kind = img_kinds.colors;
    img_title = '彩色图三通道直方图';

  case 4
    % TODO: support png???
    img_kind = img_kinds.opacity;
    img_title = '彩色图四通道直方图（含透明通道）'
  
  otherwise
    fprintf('不支持输出此类图片的直方图\r\n');
    
    % exit this process
    return;
end

figure('name', img_title);
title(img_title);
for i = 1:img_c
  subplot(1, img_c, i);
  imhist(img_data(:, :, i));
  title(img_kind(i));
end
